package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.vo.SkuSaveVo;
import com.atguigu.gmall.search.Goods;
import com.atguigu.gmall.starter.cache.service.CacheService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class                                                                     SkuController {
    @Autowired
    private SpuImageService  spuImageService;
    @Autowired
    private SpuSaleAttrService  spuSaleAttrService;
    @Autowired
    private SkuInfoService  skuInfoService;
    @Autowired
    CacheService cacheService;

    /**
     * 商品上架
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result up(@PathVariable("skuId") Long skuId ){
        skuInfoService.upgoods(skuId);
        return  Result.ok();
    }

    /**
     * 商品下架
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result down(@PathVariable Long skuId){
        skuInfoService.downGoods(skuId);
        return  Result.ok();

    }

    @GetMapping("/updateSkuInfo")
    public Result updateSkuInfo(@RequestBody SkuSaveVo skuSaveVo ){
        //1修改数据库
        //2删除缓存
        cacheService.deleteDoubleCache("skuinfo");
        return  Result.ok();
    }

    /**
     * 获取spu图片列表
     * @param spuId
     * @return
     */
   @GetMapping("/spuImageList/{spuId}")
    public Result  spuImageList(@PathVariable Long spuId){
       List<SpuImage> list = spuImageService.lambdaQuery().eq(SpuImage::getSpuId,spuId).list();
         return  Result.ok(list);
   }

    /**
     * 查询spu所有定义的销售属性
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
     public  Result spuSaleAttrList (@PathVariable Long spuId){
      List<SpuSaleAttr>  list=  spuSaleAttrService.getSpuSaleAttrList(spuId);
       return   Result.ok(list);
    }

    /**
     *  保存 sk列表
     * @param
     * @return
     */
    @ApiOperation("保存sku列表")
    @PostMapping("/saveSkuInfo")
    public Result saveSkuSaleArr(@RequestBody SkuSaveVo vo){
        skuInfoService.saveSkuData(vo);
       return  Result.ok();
    }

  @GetMapping("/list/{page}/{limit}")
    public  Result PageSkuList(@PathVariable("page") Long page , @PathVariable("limit") Long limit){
      Page<SkuInfo> pages = skuInfoService.page(new Page<SkuInfo>(page, limit));
      return   Result.ok(pages);
    }
}
